package com.speedchina.permission.web.sys;

import com.speedchina.framework.base.QueryModel;
import com.speedchina.framework.helper.Assert;
import com.speedchina.framework.model.R;
import com.speedchina.framework.validator.ValidatorHelper;
import com.speedchina.framework.validator.group.AddGroup;
import com.speedchina.framework.validator.group.UpdateGroup;
import com.speedchina.permission.base.constant.SysConstant;
import com.speedchina.permission.base.i18n.I18nService;
import com.speedchina.permission.domain.sys.po.UserResourcePo;
import com.speedchina.permission.service.sys.UserResourceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *  接口
 * @author Winter
 * @date 2021-04-08 09:37:22
 * @deprecated 中间表 可不用 controller
 */
@RestController
@RequestMapping("/sys/userResource")
@Api(description = " 接口")
public class UserResourceController {
    
    @Autowired
    private I18nService i18nService;

    @Autowired
    private UserResourceService service;

    @ApiOperation("分页查询")
    @GetMapping
    // @SysLog("分页查询-")
    // @RequiresPermissions(":userResource:list")
    public R query(Integer pageNo, Integer pageSize, UserResourcePo params) {
        return R.page(service.queryPage(new QueryModel<>(pageNo, pageSize, params)));
    }

    @ApiOperation("详情查询")
    @GetMapping("/get")
    // @SysLog("详情查询-")
    // @RequiresPermissions(":userResource:info")
    public R get(UserResourcePo params) {
        return R.data(service.get(params));
    }

    @ApiOperation("新增")
    @ApiImplicitParams({
            // @ApiImplicitParam(name = "", value = "", dataType = "String", paramType = "path")
    })
    @PostMapping
    // @SysLog("新增-")
    // @RequiresPermissions(":userResource:add")
    public R add(@RequestBody UserResourcePo entity) {
        ValidatorHelper.validateEntity(entity, AddGroup.class);
        return R.data(service.save(entity));
    }

    @ApiOperation("修改")
    @ApiImplicitParams({
            // @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", paramType = "path")
    })
    @PutMapping
    // @SysLog("修改-")
    // @RequiresPermissions(":userResource:update")
    public R update(@RequestBody UserResourcePo entity) {
        ValidatorHelper.validateEntity(entity, UpdateGroup.class);
        return R.data(service.updateById(entity));
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/{ids}")
    // @SysLog("批量删除-")
    // @RequiresPermissions(":userResource:delete")
    public R delete(@PathVariable("ids") String strIds) {
        Assert.isNull(strIds, i18nService.getMessage("DELETE_CONDITION_ILLEGAL"));
        List<Serializable> ids = new ArrayList<>();
        for (String id : strIds.split(SysConstant.SPLIT_REGEX)) {
            if (StringUtils.isNotBlank(id)) {
                ids.add(id);
            }
        }
        Assert.isTrue(ids.isEmpty(), i18nService.getMessage("DELETE_CONDITION_ILLEGAL"));
        return R.data(service.deleteBatch(ids));
    }

}